package com.jhsw.roadsun.module.business.controller.app.expressage;

import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.idempotent.core.annotation.Idempotent;
import com.jhsw.roadsun.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.jhsw.roadsun.framework.security.core.util.SecurityFrameworkUtils;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressagePageReqVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressageRespVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressageSaveReqVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressageSimpleRespVO;
import com.jhsw.roadsun.module.business.service.expressage.ExpressageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;

import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 寄件信息")
@RestController
@RequestMapping("/biz/expressage")
@Validated
public class AppExpressageController {

    @Resource
    private ExpressageService expressageService;

    @PostMapping("/create")
    @Operation(summary = "创建寄件信息")
    public CommonResult<Long> createExpressage(@Valid @RequestBody AppExpressageSaveReqVO createReqVO) {
        return success(expressageService.appCreateExpressage(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除寄件信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteExpressage(@RequestParam("id") Long id) {
        expressageService.appDeleteExpressage(id, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得寄件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppExpressageRespVO> getExpressage(@RequestParam("id") Long id) {
        return success(expressageService.appGetExpressage(id, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PostMapping("/download/{type}/{id}")
    @Operation(summary = "下载文件")
    @Idempotent(timeout = 3, message = "您的手速太快啦！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<String> downloadFile(@PathVariable("type") Integer type, @PathVariable("id") Long id) {
        return success(expressageService.downloadFile(type, Collections.singletonList(id)));
    }

    @GetMapping("/page")
    @Operation(summary = "获得寄件信息分页")
    public CommonResult<PageResult<AppExpressageSimpleRespVO>> getExpressagePage(@Valid AppExpressagePageReqVO pageReqVO) {
        return success(expressageService.appGetExpressagePage(pageReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }
}